<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text" placeholder="请输入带有小数的数字" class="input1">
    <input type="text" placeholder="请输入将要保留的小数位数" class="input2">
    <div style="width: 150px;min-height: 20px;border:1px skyblue solid;"></div>
    <button>点我</button>
    <script >
        let input1=document.querySelector('.input1')
        let input2=document.querySelector('.input2')
        let box=document.querySelector('div')
        let btn=document.querySelector('button')
        btn.onclick=()=>{
            if(!input1.value||!Number(input1.value)){
                alert('请输入带有小数的数字')
                return
            }
            if(!input2.value){
                alert('请输入将要保留的小数位数')
                return
            }

            let newnum=Number(input1.value).toFixed(Number(input2.value)+1)
            box.innerHTML=newnum.substring(0,newnum.length-1)
        }

        input1.onkeyup=function(){
            if(!Number(input1.value)){
                alert('非法内容')
                input1.value=''
            }
        }
        input2.onkeyup=function(){
            if(!Number(input2.value)){
                alert('非法内容')
                input2.value=''
            }
        }
        


    </script>
</body>
</html>